package com.codedancer.campus.client.api.headlineNews;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.headlineNews.ro.HeadlineNewsPageSearchRo;
import com.codedancer.campus.client.service.IHeadlineNewsService;
import com.codedancer.campus.client.service.bo.headlineNews.HeadlineNewsPageSearchBo;
import com.codedancer.campus.client.service.vo.headlineNews.HeadlineNewsPageVo;
import com.codedancer.campus.client.service.vo.headlineNews.HeadlineNewsVo;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "头条新闻Api", description = "头条新闻Api", tags = "头条新闻Api")
@RequestMapping("/api/headline-news")
public class HeadlineNewsApi extends BaseApi {

    @Autowired
    private IHeadlineNewsService headlineNewsService;

    @GetMapping("/page/{type}")
    @ApiOperation("分页查询头条新闻")
    public ApiResponse<IPage<HeadlineNewsPageVo>> page(
        @ApiParam(value = "头条新闻分页查询Ro", required = false) HeadlineNewsPageSearchRo headlineNewsPageSearchRo,
        @PathVariable(value = "type", required = true) @ApiParam(value = "头条新闻类型 1表示本地新闻 2表示其他新闻", required = true) Integer type,
        HttpServletRequest request) {
        HeadlineNewsPageSearchBo headlineNewsPageSearchBo = BeanUtil.copyProperties(
            headlineNewsPageSearchRo, HeadlineNewsPageSearchBo.class);
        headlineNewsPageSearchBo.setType(type);
        return success(headlineNewsService.page(request, headlineNewsPageSearchBo));
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("头条新闻详情")
    public ApiResponse<HeadlineNewsVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "头条新闻uuid", required = true) String uuid) {
        return success(headlineNewsService.detail(uuid));
    }
}
